package com.scm.biz.service._impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scm.biz.bean.MsgNotice;
import com.scm.biz.bean.SysRole;
import com.scm.biz.dao.MsgNoticeDao;
import com.scm.biz.dao.SysRoleDao;
import com.scm.biz.dao.SysUserDao;
import com.scm.biz.dto.MsgNoticeDto;
import com.scm.biz.enums.OrderStatusEnum;
import com.scm.biz.enums.OrderTypeEnum;
import com.scm.biz.query.MsgNoticeQuery;
import com.scm.biz.query.OrderDynamicQuery;
import com.scm.biz.result.MsgNoticeResult;
import com.scm.biz.service.MsgNoticeService;
import com.scm.biz.service.SysRoleService;
import com.scm.utils.UfdmDateUtil;

/**
 * 工作台消息模块业务层接口实现类
 * 
 * @author 张乐锋
 *
 */
@Service
public class MsgNoticeServiceImpl implements MsgNoticeService {

	@Resource
	private MsgNoticeDao msgNoticeDao;
	@Autowired
	SysRoleDao sysRoleDao;

	@Override
	public List<MsgNoticeResult> getMsgNotice(MsgNoticeQuery msgNoticeQuery) {
		List<MsgNoticeResult> msgNoticeResults = msgNoticeDao.selectMsgNotice(msgNoticeQuery);
		return msgNoticeResults;
	}

	@Override
	public int getTotal(MsgNoticeQuery msgNoticeQuery) {
		int total = msgNoticeDao.selectTotal(msgNoticeQuery);
		return total;
	}

	@Override
	public int getAllOrderTotal() {
		int designTotal = msgNoticeDao.selectDesignTotal();
		int proofingTotal = msgNoticeDao.selectProofingTotal();
		int productionTotal = msgNoticeDao.selectProductionTotal();
		int closeDesignTotal = msgNoticeDao.selectCloseDesignTotal();
		int closeProofingTotal = msgNoticeDao.selectCloseProofingTotal();
		int closeProductionTotal = msgNoticeDao.selectCloseProductionTotal();
		return designTotal + proofingTotal + productionTotal + closeDesignTotal + closeProofingTotal
				+ closeProductionTotal;
	}

	@Override
	public int getDesignOrderTotal() {
		int designTotal = msgNoticeDao.selectDesignTotal();
		return designTotal;
	}

	@Override
	public int getProofingOrderTotal() {
		int proofingTotal = msgNoticeDao.selectProofingTotal();
		return proofingTotal;
	}

	@Override
	public int getProductionOrderTotal() {
		int productionTotal = msgNoticeDao.selectProductionTotal();
		return productionTotal;
	}

	@Override
	public int modifyStatus(Long id) {
		int i = msgNoticeDao.updateStatus(id);
		return i;
	}

	@Override
	public List<MsgNoticeResult> getOrderDynamic5(Integer userId) {
		List<MsgNoticeResult> msgNoticeResults = msgNoticeDao.selectOrderDynamic5(userId);
		return msgNoticeResults;
	}

	@Override
	public List<MsgNoticeResult> getOrderDynamic(OrderDynamicQuery orderDynamicQuery) {
		List<MsgNoticeResult> msgNoticeResults = msgNoticeDao.selectOrderDynamic(orderDynamicQuery);
		return msgNoticeResults;
	}

	@Override
	public int getOrderDynamicTotal(OrderDynamicQuery orderDynamicQuery) {
		int total = msgNoticeDao.selectOrderDynamicTotal(orderDynamicQuery);
		return total;
	}

	public void insertOpMsg(Integer orderStatus,String orderType, Long taskId, Date enddate, String customerName,
			Integer... performers) {
//		Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
//
//		if (orderStatus== 10) {
//			// 创建设计订单
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"新的设计订单", sysUserDao.selectByPrimaryKey(userId).getName() + "创建了" + customerName
//								+ "客户的设计订单，请尽快安排设计，截止时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		} else if (orderStatus == 11) {
//			// 分配任务
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"新的设计订单", sysUserDao.selectByPrimaryKey(userId).getName() + "指派了" + customerName
//								+ "客户的设计订单给你，请尽快设计，截止时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		} else if (orderStatus == 12) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"设计订单待审核", sysUserDao.selectByPrimaryKey(userId).getName() + "提交了" + customerName
//								+ "客户的的设计稿，请尽快审核",
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 13) {
//			// 设计稿已完成
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"设计稿已完成", customerName
//								+ "客户的设计稿已完成，请尽快联系客户",
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 99 && orderType.equals("T1")) {
//			// 设计稿已通过选款
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"设计稿已通过选款", sysUserDao.selectByPrimaryKey(userId).getName() + "提交了" + customerName
//								+ "客户的设计稿已通过选稿，进入打样流程",
//						taskId, enddate, performer);
//			}
//			
//		}else if (orderStatus == 100 && orderType.equals("T1")) {
//			// 设计稿未通过选款
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"设计稿未通过选款", sysUserDao.selectByPrimaryKey(userId).getName() + "提交了" + customerName
//								+ "客户的设计稿未通过选稿",
//						taskId, enddate, performer);
//			}
//			
//		}else if (orderStatus == 20) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"新的打样订单", sysUserDao.selectByPrimaryKey(userId).getName() + "创建了" + customerName
//								+ "客户的打样订单，请尽快安排工厂，截止时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 201) {
//			// 项目经理安排工厂
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"工厂已安排，请上传尺码表", sysUserDao.selectByPrimaryKey(userId).getName() + "已经安排了" + customerName
//								+ "客户的打样工厂，请尽快上传尺码表，截止时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 202) {
//			// 上传尺码表
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"尺码表已上传，请安排工厂", sysUserDao.selectByPrimaryKey(userId).getName() + "已上传了" + customerName
//								+ "客户的打样订单尺码表，请尽快安排工厂，截止时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 202) {
//			// 上传尺码表
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"尺码表已上传，请安排工厂", sysUserDao.selectByPrimaryKey(userId).getName() + "已上传了" + customerName
//								+ "客户的打样订单尺码表，请尽快安排工厂，截止时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 21) {
//			// 上传尺码表
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"有新的打样订单已安排", customerName
//								+ "客户的打样订单已安排工厂生产，请做好批版准备" ,
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 22) {
//			// 上传尺码表
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"打样订单批版通过", sysUserDao.selectByPrimaryKey(userId).getName() + "销售的" + customerName
//								+ "客户的打样订单已通过批版，请报价",
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 23) {
//			// 上传尺码表
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"打样订单的样衣已报价", sysUserDao.selectByPrimaryKey(userId).getName() + "销售的" + customerName
//								+ "客户的打样订单已报价",
//						taskId, enddate, performer);
//			}
//			
//		}else if (orderStatus == 99 && orderType.equals("T2")) {
//			// 上传尺码表
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"样衣已通过选款",  customerName
//								+ "客户的样衣已通过选稿，进入大货流程",
//						taskId, enddate, performer);
//			}
//			
//		}else if (orderStatus == 100 && orderType.equals("T2")) {
//			// 上传尺码表
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"样衣未通过选款",  customerName
//								+ "客户的样衣未通过选稿",
//						taskId, enddate, performer);
//			}
//			
//		}else if (orderStatus == 30) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"新的大货订单", sysUserDao.selectByPrimaryKey(userId).getName() + "创建了" + customerName
//								+ "客户的大货订单，请尽快安排工厂，截止时间为11月13日" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 301) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"工厂已安排，请上传推码表", sysUserDao.selectByPrimaryKey(userId).getName() + "已经安排了" + customerName
//								+ "客户的大货工厂，请尽快上传推码表，截止时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 302) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"工厂已安排", sysUserDao.selectByPrimaryKey(userId).getName() + "已经安排了" + customerName
//								+ "客户的大货（编号）工厂" ,
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 31) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"推码表已上传", sysUserDao.selectByPrimaryKey(userId).getName() + "已经上传" + customerName
//								+ "客户的大货订单推码表，请跟进项目进度" ,
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 321) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"大货已开始生产", customerName
//								+ "客户的大货订单已经开始生产，时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 322) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"大货已完成首件样",  customerName
//								+ "客户的大货订单已经完成首件样，时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 323) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"大货已完成生产", "",
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 323) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"大货质检通过", customerName
//								+ "客户的大货订单已经完成生产，时间为" + UfdmDateUtil.dateToString(enddate, "MM月dd日"),
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 32) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"大货质检通过", customerName
//								+ "客户的大货订单质检已通过，请联系工厂填写装箱单" ,
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 33) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"工厂已发货", customerName
//								+ "客户的大货订单已发货，快递单号为xxxxxxx" ,
//						taskId, enddate, performer);
//			}
//		}else if (orderStatus == 34) {
//			// 提交设计稿
//			for (Integer performer : performers) {
//				this.insert(orderStatus,orderType,
//						"大货已送达", customerName
//								+ "客户的大货订单已送达，到货时间为11月14日，请联系客户付款" ,
//						taskId, enddate, performer);
//			}
//		}
	}

	public void insert(MsgNotice msgNotice) {

		// 创建设计订单
//		Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
//		MsgNotice msgNotice = new MsgNotice();
//		msgNotice.setTitle(title);
//		msgNotice.setContent(content);
//		msgNotice.setEnddate(enddate);// 一个星期
//		msgNotice.setCreator(Long.valueOf(userId));
//		msgNotice.setCreateAt(new Date());
//		msgNotice.setTaskId(taskId);
//		msgNotice.setUpdateAt(new Date());
//		msgNotice.setUserId(performer);
//		msgNotice.setStatus(0);
//		msgNotice.setTaskStatus(taskStatus);
//		msgNoticeDao.insert(msgNotice);

	}

	@Override
	public void insert(MsgNoticeDto msgNoticeDto,String roleCodes) {
		Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
		
		for(String code:roleCodes.split(",")){
			SysRole sysRole = sysRoleDao.selectByCode(code);
			MsgNotice msgNotice = new MsgNotice();
			msgNotice.setContent(msgNoticeDto.getContent());
			msgNotice.setCreateAt(new Date());
			msgNotice.setTitle(msgNoticeDto.getTitle());
			msgNotice.setCreator(Long.valueOf(userId));
			msgNotice.setEnddate(msgNoticeDto.getEnddate());
			msgNotice.setStatus(0);
			msgNotice.setTaskId(msgNoticeDto.getTaskId());
			msgNotice.setOrderType(msgNoticeDto.getOrderType());
			msgNotice.setUserId(sysRole.getId());
			msgNotice.setOrderId(msgNoticeDto.getOrderId());
			msgNotice.setUserType(msgNoticeDto.getUserType());
			msgNoticeDao.insert(msgNotice);
		}
		
	}
	@Override
	public void insert(MsgNoticeDto msgNoticeDto,Integer assigner) {
		Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
		
		MsgNotice msgNotice = new MsgNotice();
		msgNotice.setContent(msgNoticeDto.getContent());
		msgNotice.setCreateAt(new Date());
		msgNotice.setTitle(msgNoticeDto.getTitle());
		msgNotice.setOrderId(msgNoticeDto.getOrderId());
		msgNotice.setCreator(Long.valueOf(userId));
		msgNotice.setEnddate(msgNoticeDto.getEnddate());
		msgNotice.setStatus(0);
		msgNotice.setTaskId(msgNoticeDto.getTaskId());
		msgNotice.setOrderType(msgNoticeDto.getOrderType());
		msgNotice.setUserId(assigner);
		msgNotice.setUserType(msgNoticeDto.getUserType());
		msgNoticeDao.insert(msgNotice);
		
	}


}
